export default {
  namespaced: true,
  state: {
    token: uni.getStorageSync('token') ||
      'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjkxLCJpYXQiOjE2NTkxNDc2OTAsImV4cCI6MTAwMTY1OTE0NzY4OX0.lQIHvDa85y46kaoKOHauv0rBF2osirrI3BtGsVtV-iQ'
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      uni.setStorageSync('token', token)
    }
  },
  actions: {
    async wxLogin({ commit }) {
      try {
        const { encryptedData, iv, rawData, signature, userInfo } = await wx.getUserProfile({ desc: '获取用户信息用登录' })
        const { code } = await wx.login()
        const data = { code, encryptedData, iv, rawData, signature }
        const res = await uni.$u.http.post('/users/wxlogin', data)
        // console.log(res)
        const { token } = res.data.message
        commit('setToken', token)
        uni.showToast({ icon: 'success', title: '登录成功~' })
      } catch (error) {
        uni.showToast({ icon: 'error', title: '登录失败~' })
      }
    }
  },
  getters: {}
}